package com.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.SystemInfo.RecordBean;
import com.project.bean.SystemInfo.UserBean;
import com.project.service.IRecordService;
import com.project.service.IUserService;
import com.project.until.JWTUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {


    @Resource
    private IUserService userService;


    @Resource
    private IRecordService recordService;

    @RequestMapping("Login")

    public UserBean Login(String userName, String userPwd, HttpServletResponse response) {

        UserBean userBean = userService.Login(userName, userPwd);

        if (userBean != null) {
            RecordBean recordBean = new RecordBean();
            String msg = "用户" + userBean.getUserName() + "在" + LocalTime.now() + "登陆了系统";
            recordBean.setRecordName(msg);
            recordBean.setRecordTime(LocalDateTime.now());
            recordService.addRecord(recordBean);
        }

        if (userBean != null) {
            Map map = new HashMap<>();
            map.put("userId", userBean.getUserId() + "");
            map.put("userName", userName);
            map.put("userPwd", userPwd);
            map.put("userRoot", userBean.getUserRoot());


            String token = new JWTUtil().createToken(map);

            response.setHeader("token", token);

            return userBean;
        }

        return userBean;


    }

    @RequestMapping("findAllUser")

    public IPage<UserBean> findAllUser(@RequestParam(value = "pageNo",defaultValue = "1") int pageNo, @RequestParam(value = "pageSize",defaultValue = "3") int pageSize, String userRoot) {

        IPage<UserBean> page = userService.findCutUser(pageNo, pageSize, userRoot);

        return page;

    }

    ;


    @RequestMapping("addUser")
    public String addUser(UserBean userBean) {

        if (userBean != null) {
            userService.addUser(userBean);
        } else {
            return "no";
        }
        return "ok";

    }


    @RequestMapping("delUser")

    public String delUser(int userId) {

        userService.delUser(userId);

        return "ok";
    }


    @RequestMapping("findUserById")
    public UserBean findUser(int userId) {

        UserBean userBean = userService.findUser(userId);

        return userBean;

    }

    @RequestMapping("updateUser")
    public String updateUser(int userId, String userPwd,String userRoot) {

        userService.updateUser(userId, userPwd,userRoot);

        return "ok";
    }

    @RequestMapping("check")
    public String checkName(String name) {

        List<UserBean> list = userService.findAllUser();

        for (UserBean u : list) {

            if (name.equals(u.getUserName())) {
                return "no";
            }

        }

        return "yes";
    }


    @RequestMapping("getLoginUser")
    public UserBean getLoginUser(HttpServletRequest request) {
        String token = request.getHeader("token");
        // 利用工具类对其进行解码
        String userName = new JWTUtil().verify(token, "userName");
        String userPwd = new JWTUtil().verify(token, "userPwd");
        int userId = Integer.parseInt(new JWTUtil().verify(token, "userId"));
        String userRoot = new JWTUtil().verify(token, "userRoot");
        UserBean userBean = new UserBean(userId, userName, userPwd, userRoot);
        return userBean;

    }


}
